const express = require("express")

const shopRouter = require("./router/shopRouter")
const usersRouter = require("./router/usersRouter")
const smallRouter = require("./router/smallRouter")
const dogRouter = require("./router/dogRouter")
const globalRouter = require("./router/globalRouter")
const fishRouter = require("./router/fishRouter")
const catRouter = require("./router/catRouter")
const cartRouter = require("./router/cartRouter")

// 处理请求体中post传递的参数
const bodyParser = require("body-parser")

//创建一个服务器应用
const app = express()
const port = 3000

// 处理跨域
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
});

//处理静态资源请求
app.use(express.static('public'))

//拦截所有请求
//extended:false 方法内部使用querystring模块处理请求参数的格式
//extended:true 方法内部使用第三方模块qs处理请求参数的格式
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
    // app.get("/log",function(req,res){
    //     console.log(222);
    // })

app.use("/boqishop", shopRouter);
app.use("/register", usersRouter);
app.use("/small", smallRouter);
app.use("/dog", dogRouter);
app.use("/boqishop", globalRouter)
app.use("/fish", fishRouter)
app.use("/cat", catRouter)
app.use("/cart", cartRouter)

//路由
app.listen(port, () => {
    console.log(`服务器已经启动在${port}端口`)
})